//
//  RecordViewController.m
//  JiFengDaRen
//
//  Created by shouhan on 2018/6/8.
//  Copyright © 2018年 陈永发. All rights reserved.
//

#import "RecordViewController.h"
#import "RecordTableViewCell.h"
#import "JCPickerView.h"
#import "RecordModel.h"
#import "SubModel.h"
#import "RecordDetailsViewController.h"
@interface RecordViewController ()<UITableViewDelegate,UITableViewDataSource,JCPickerViewDelegate>
{
    NSInteger pageindex;
}
@property (nonatomic, strong) UITableView * recordTableView;

@property (nonatomic, strong) NSMutableArray * dataSource;

@property (nonatomic, strong) JCPickerView * dateView;//时间选择器
@property (nonatomic, strong) NSString * benginDate;// 开始日期
@property (nonatomic, strong) NSString * endDate;// 结束日期

@property (nonatomic, strong) RecordModel * recordModel;
@end

@implementation RecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"交易记录";
    self.dataSource = [NSMutableArray array];
    pageindex = 1;
    [self initBarItem];
    
    [self recordTableView];
    
    self.recordTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    
    self.recordTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    
}

- (void)initBarItem{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    }else{
        return self.dataSource.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            SearchTableViewCell * cell = [SearchTableViewCell tableViewCellWithTableView:tableView];
            cell.model = self.recordModel;
            return cell;
        }else{
            DateTableViewCell * cell = [DateTableViewCell tableViewCellWithTableView:tableView];
            [cell getCellIndex:indexPath.row];
            return cell;
        }
    }else{
       
        RecordTableViewCell * cell = [RecordTableViewCell tableViewCellWithTableView:tableView];
        cell.model = self.dataSource[indexPath.row];
        return cell;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return __I6Height(50);
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        if (indexPath.row != 0) {
            self.dateView= [[JCPickerView alloc] initDatePickerWithDefaultDate:nil andDatePickerMode:UIDatePickerModeDate];
            self.dateView.dateTag = indexPath.row;
            self.dateView.datePicker.maximumDate = [NSDate date];

            self.dateView.delegate = self;
            [self.dateView show];
    
        }
    }else{
        SubModel *model = self.dataSource[indexPath.row];
        RecordDetailsViewController * VC = [[RecordDetailsViewController alloc] init];
        VC.benginDate = self.benginDate;
        VC.endDate = self.endDate;
        VC.phone = model.phone;
        [self.navigationController pushViewController:VC animated:YES];
    }
}

- (void)pickerView:(JCPickerView *)pickerView didSelectDateString:(NSString *)dateString{
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    if (self.dateView.dateTag == 1) {
        //比较时间
        int result = [Tool compareOneDay:[dateFormatter dateFromString:dateString] withAnotherDay:[dateFormatter dateFromString:self.endDate]];

        if (result == 1) {
            [Tool alertWithMessage:@"起始日期大于终止日期" who:self];
            return;
        }
        self.benginDate = dateString;
    }else{

        int result =  [Tool compareOneDay:[dateFormatter dateFromString:dateString] withAnotherDay:[dateFormatter dateFromString:self.benginDate]];
        if (result == -1) {
            [Tool alertWithMessage:@"终止日期小于起始日期" who:self];
            return;
        }
        self.endDate = dateString;
    }
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.dateView.dateTag inSection:0];
    DateTableViewCell *cell = [self.recordTableView cellForRowAtIndexPath:indexPath];
    
    cell.date.text = dateString;

}



// 懒加载
- (UITableView *)recordTableView{
    if (!_recordTableView) {
        _recordTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-KNAVIGATIONBARHEIGHT) style:UITableViewStyleGrouped];
        _recordTableView.delegate = self;
        _recordTableView.dataSource = self;
        _recordTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _recordTableView.backgroundColor = [UIColor colorWithHexString:@"#F7F8FB"];
        
        [self.view addSubview:_recordTableView];
    }
    return _recordTableView;
}

-(void)refresh
{
    pageindex = 1;
    [self DoRequestQuery_trade:YES];
}
-(void)loadMore
{
    pageindex ++;
    [self DoRequestQuery_trade:NO];
}

- (void)DoRequestQuery_trade:(BOOL)isRemove{
    
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    [dict setValue:self.benginDate forKey:@"startTime"];
    [dict setValue:self.endDate forKey:@"endTime"];
    [dict setValue:[UserModel shareManager].phone forKey:@"cooperator"];
    [dict setValue:[NSString stringWithFormat:@"%ld",(long)pageindex] forKey:@"pageindex"];
    [dict setValue:@"10" forKey:@"count"];

    [PPNetworkHelper POST:Query_trade parameters:dict cache:NO encrypt:NO decode:YES responseCache:^(id responseCache) {
        
    } success:^(id result) {
        if (isRemove) {
            [self.dataSource removeAllObjects];
        }
        if ([result[@"success"] boolValue] == YES) {
            self.recordModel = [RecordModel mj_objectWithKeyValues:result[@"data"]];
            [self.dataSource addObjectsFromArray:self.recordModel.sub];
        }else{
            [self.view makeToast:result[@"data"] duration:2.f position:@"center"];
        }
        [self.recordTableView.mj_header endRefreshing];
        [self.recordTableView.mj_footer endRefreshing];
        [self.recordTableView reloadData];
    } failure:^(NSError *error) {
        [self.recordTableView.mj_header endRefreshing];
        [self.recordTableView.mj_footer endRefreshing];
    }];
}

- (void)searchAction{
    
    if (self.benginDate.length == 0) {
        [self.view makeToast:@"请选择还款起始日期" duration:2.f position:@"center"];
    }else if (self.endDate.length == 0){
        [self.view makeToast:@"请选择还款终止日期" duration:2.f position:@"center"];
    }else{
        [self DoRequestQuery_trade:YES];
    }


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
